/*
 * @Author: silence
 * @Date: 2023-07-03 11:56:40
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-31 14:41:09
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:gif_view/gif_view.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../utils/color/ColorUtils.dart';

enum HomeEnergyPosition {
  TopLeft,
  TopRight,
  BottomLeft,
  BottomRight,
}

class HomeEnergyItem extends StatefulWidget {
  final HomeEnergyPosition position;
  final String? title;
  final String imgPath;

  /// 功能值
  final String? value;

  /// 动图线
  final String lineGifPath;

  /// 是否显示详情标志，默认不显示
  final bool isShowDetailFlag;

  const HomeEnergyItem(
      {this.position = HomeEnergyPosition.TopLeft,
      this.title,
      required this.imgPath,
      this.value,
      required this.lineGifPath,
      super.key,
      this.isShowDetailFlag = false});

  @override
  State<HomeEnergyItem> createState() => _HomeEnergyItemState();
}

class _HomeEnergyItemState extends State<HomeEnergyItem> {
  double _lineGifLeftMargin = 0;
  double _lineGifTopMargin = 110.w;
  double _energyViewTopMargin = 20.w;
  double _titleViewTopMargin = 0;

  @override
  void initState() {
    super.initState();

    switch (widget.position) {
      case HomeEnergyPosition.TopLeft:
        _titleViewTopMargin = 0;
        _lineGifLeftMargin = 50.w;
        _lineGifTopMargin = 110.w;
        _energyViewTopMargin = 20.w;
        break;

      case HomeEnergyPosition.TopRight:
        _titleViewTopMargin = 0;
        _lineGifLeftMargin = 0;
        _lineGifTopMargin = 110.w;
        _energyViewTopMargin = 20.w;
        break;

      case HomeEnergyPosition.BottomLeft:
        _titleViewTopMargin = 160.w;
        _lineGifLeftMargin = 50.w;
        _lineGifTopMargin = 10.w;
        _energyViewTopMargin = 63.w;
        break;

      case HomeEnergyPosition.BottomRight:
        _titleViewTopMargin = 160.w;
        _lineGifLeftMargin = 0;
        _lineGifTopMargin = 10.w;
        _energyViewTopMargin = 63.w;
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 120.w,
      height: 180.w,
      child: Stack(
        alignment: AlignmentDirectional.topCenter,
        children: [
          /// 动图线
          Positioned(
            child: lineGifView(),
            top: _lineGifTopMargin,
            left: _lineGifLeftMargin,
          ),

          /// 标题
          Positioned(top: _titleViewTopMargin, child: titleView()),

          /// 能耗
          Positioned(
            child: energyView(),
            top: _energyViewTopMargin,
          ),
        ],
      ),
    );
  }

  titleView() {
    return Container(
      child: Row(
        children: [
          SilText(
            widget.title,
            color: ColorUtils.color_theme,
            fontSize: 14.sp,
            maxLines: 0,
            textAlign: TextAlign.center,
          ),
          if (widget.isShowDetailFlag)
            SilImage(
              'imgs/common/common_info.png',
              margin: EdgeInsets.only(left: 5.w),
              height: 12.w,
              width: 12.w,
            )
        ],
      ),
    );
  }

  energyView() {
    return Stack(
      alignment: AlignmentDirectional.center,
      children: [
        SilImage(
          widget.imgPath,
          height: 90.w,
          width: 90.w,
        ),
        Positioned(
            top: 48.w,
            child: Container(
                width: 100.w,
                height: 36.w,
                padding: EdgeInsets.symmetric(horizontal: 20.w),
                alignment: Alignment.center,
                // color: ColorUtils.color_theme_03,
                child: SilText(
                  widget.value,
                  color: ColorUtils.color_font_main,
                  fontSize: 14.sp,
                  textAlign: TextAlign.center,
                  maxLines: 0,
                ))),
      ],
    );
  }

  /// 动图线
  lineGifView() {
    return Container(
      child: GifView(
        image: AssetImage(widget.lineGifPath),
        height: 56.w,
        width: 75.w,
      ),
    );
  }
}
